package com.gz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gz.dao.TuserMapper;
import com.gz.dto.PageData;
import com.gz.dto.TuserDto;
import com.gz.dto.TuserQueryDto;
import com.gz.entity.*;
import com.gz.service.TuserService;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户表(Tuser)表服务实现类
 *
 * @author makejava
 * @since 2024-01-15 14:25:06
 */
@Service
public class TuserServiceImpl implements TuserService {
    @Autowired
    private TuserMapper tuserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Tuser queryById(Integer id) {
        return tuserDao.queryById(id);
    }
    @Override
    public List<Teacher> querySelByName(String realName){
        return tuserDao.querySelByName(realName);
    }
    @Override
    public List<Tuser> queryAll(TuserDto user) {
        return tuserDao.queryAll(user);
    }
    @Override
    public List<Tuser> queryUserByRoleId(String roleId) {
        return tuserDao.queryTUserByRole(roleId);
    }
    public PageData queryPageTeachers(TuserQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<Tuser> list = tuserDao.queryTUsers(dto);
        PageInfo<Tuser> pageInfo = new PageInfo<>(list);
        return new PageData<>(pageInfo.getTotal(), list);
    }
    @Override
    public List<SelData> querySelTUser(String roleId){
        return tuserDao.querySelTUser(roleId);
    }
    @Override
    public List<RoleInfo> queryUserRole(String id) {
        return tuserDao.queryUserRoleInfo(id);
    }

    /**
     * 分页查询
     *
     * @param dto 筛选条件
     * @return 查询结果
     */
    @Override
    public PageData queryByPage(TuserDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<Tuser> list = tuserDao.queryAllByLimit(dto);
        PageInfo<Tuser> pageInfo = new PageInfo<>(list);
        return new PageData<>(pageInfo.getTotal(), list);
    }

    /**
     * 新增数据
     *
     * @param tuser 实例对象
     * @return 实例对象
     */
    @Override
    public Tuser insert(Tuser tuser) {
        tuser.setCrtDt(new Date());
        tuser.setOptDt(new Date());
        tuserDao.insert(tuser);
        return tuser;
    }

    /**
     * 修改数据
     *
     * @param tuser 实例对象
     * @return 实例对象
     */
    @Override
    public Tuser update(Tuser tuser) {
        tuserDao.update(tuser);
        return queryById(tuser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return tuserDao.deleteById(id) > 0;
    }
}
